<?php

class Tick
{
    //properties
    private $TickNumber = 0;   //what tick number is this?
    private $SliceNumber = 0;  //what slice is this on the wheel?
    public $ListOfTokenIDs = array();
    
    public function GetTickNumber()
    {
        return $this->TickNumber;
    }
    
    public function GetSliceNumber()
    {
        return $this->SliceNumber;
    }
    
    public function SetTickNumber($newValue)
    {
        $this->TickNumber = $newValue;
        $this->SliceNumber = $newValue % 7;
    }

    //return negative if the first one is smaller, pos if the second
    //one is smaller and zero if they're equal
    static public function CompareSliceNumbers($first, $second)
    {
        return $first->GetSliceNumber() - $second->GetSliceNumber();
    }
}

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
echo "<battle xmlns=\"http://www.gardenofmadness.org/\">\r\n";

//load the battle id from the post data
$battleidtoget =  $_POST["battleid"];

//get the current tick.
$dbname = "tactickstest";
$connection = mysql_connect("tidesinreturn.gardenofmadness.org", "tacticksadmin",
                            "123abc") or die("Error connecting to SQL.");

$db = mysql_select_db($dbname, $connection);

$query = "select currenttick from BATTLES where id = '" . 
          mysql_real_escape_string($battleidtoget) . "'";


$result = mysql_query($query, $connection) or die("Could not complete database query: " . mysql_error());
$num = mysql_num_rows($result);

$currenttick = 0;
if ($num != 0)
{
    $row = mysql_fetch_array($result);
    $currenttick = $row["currenttick"];
}

//now make the current tick and the six that follow it, by
//getting the tokens on that tick count

$Ticks = array();
for ($iBuildingTick = $currenttick; $iBuildingTick < $currenttick + 7; $iBuildingTick++)
{
    $newTick = new Tick();
    //set the tick number, which also sets the slice number
    $newTick->SetTickNumber($iBuildingTick);
    //get the tokens on this tick
    $query = "select id from TOKENS where battleid = '" . $battleidtoget . "' and nexttick = '" . $iBuildingTick . "' order by id";
    $result = mysql_query($query, $connection) or die("Could not complete database query: " . mysql_error());
    $num = mysql_num_rows($result);
    if ($num != 0)
    {
        while ($row = mysql_fetch_array($result))
        {
            //this adds the id to the list tokens on the tick we're building
            $newTick->ListOfTokenIDs[] = $row["id"];
        }
    }

    $Ticks[] = $newTick;
}

//sort the tick based on their slice number.
//to make it easier for the client
usort($Ticks,array("Tick", "CompareSliceNumbers"));

foreach ($Ticks as $curr)
{
    echo "<t num='" . $curr->GetTickNumber() . "'";

    if ($curr->GetTickNumber() == $currenttick)
    {
       echo " curr='1'>\r\n";
    }
    else
    {
        echo ">\r\n";
    }

    foreach ($curr->ListOfTokenIDs as $currToken)
    {
        echo "<token id='" . $currToken . "'/>\r\n";
    }

    echo "</t>\r\n";
}

echo "</battle>";

?>
